<?php
!defined('P_W') && exit('Forbidden');
/**
 * 经纪人组服务类
 */
class PW_BrokerService {
	var $_brokerDB;
	function __construct() {
		$this->_brokerDB = $this->_getBrokerDB();
	}
	
	function PW_BrokerGroupService() {
		$this->__construct();
	}
	/**
	 * 查找经纪人
	 * @param $param	筛选条件
	 * @param $page		所属页面
	 * @param $perPage	每页显示数
	 * @return list($brokers,$count)
	 */
	function findBrokers($param = array(), $page = 1, $perPage = 20,$order = '') {
		$page = (int) $page;
		$page < 1 && $page = 1;
		$result = $this->_brokerDB->findAll($param, $page, $perPage,$order);
		foreach ($result as $key => $value) {
			$value['salenum'] = intval($value['salenum']);
			$value['hirenum'] = intval($value['hirenum']);
			$result[$key] = $this->_cookBroker($value);
		}
		
		$count = $this->_brokerDB->count($param);
		return array(
			$result,
			$count
		);
	}
	/**
	 * 获取一个经纪人信息
	 * @param $uid
	 */
	function getBroker($uid) {
		global $tdtime;
		static $brokers = array();
		if($brokers[$uid]) return $brokers[$uid];
		$brokerDB = $this->_getBrokerDB();
		$temp = $brokerDB->get($uid);
		if (!$temp) return $temp;
		$secondPostService = $this->_getSecondPostService();
		$secondPostInfo = $secondPostService->getSecondPost($uid);
		if (!$secondPostInfo) return $temp;
		
		unset($secondPostInfo['uid']);
		foreach ($secondPostInfo as $key=>$value) {
			$temp[$key] = $value;
		}
		$brokers[$uid] = $temp;
		return $temp;
	}
	/**
	 * 获取经济人领域类
	 * @param int $uid
	 */
	function getBrokerModule($uid = 0) {
		$uid = (int) $uid;
		$brokerModule = $this->_getBrokerModule();
		if (!$uid) return $this->_cookBrokerModule($brokerModule);
		
		$brokerInfo = $this->getBroker($uid);
		
		$map = $this->_getBrokerMap();
		foreach ($map as $key => $value) {
			$brokerModule->{$value} = $brokerInfo[$key];
		}
		return $this->_cookBrokerModule($brokerModule);
	}
	/**
	 * 添加一个经纪人
	 * @param $brokerModule
	 */
	function addBroker($brokerModule) {
		if (!$this->_checkBrokerModule($brokerModule)) return false;
		$tempBroker = $this->_brokerDB->get($brokerModule->uid);
		if ($tempBroker) {
			Error::addError('该经济人已存在');
			return false;
		}
		
		$data = $this->_cookBrokerModuleToDB($brokerModule);
		$this->_brokerDB->insert($data);
		
		$brokerGroupService = $this->_getBrokerGroupService();
		$brokerGroupService->addBrokerUserNum($brokerModule->groupid);
	}
	/**
	 * 修改经纪人信息
	 * @param $brokerModule
	 */
	function editBroker($brokerModule) {
		if (!$this->_checkBrokerModule($brokerModule)) return false;
		$data = $this->_cookBrokerModuleToDB($brokerModule);
		$brokerGroupService = $this->_getBrokerGroupService();
		unset($data['uid']);
		$oldBrokerInfo = $this->getBroker($brokerModule->uid);
		if($oldBrokerInfo['groupid'] != $brokerModule->groupid){
			$brokerGroupService->addBrokerUserNum($brokerModule->groupid);
			$brokerGroupService->reduceBrokerUsernum($oldBrokerInfo['groupid']);
		}
		$this->_brokerDB->update($data,$brokerModule->uid);
	}
	
	function updateBrokers($data,$uids){
		$uids = is_array($uids) ? $uids : array($uids);
		$brokerDao = House::loadDB('house_broker');
		return $brokerDao->updateMuti($data,$uids);
	}
	/**
	 * 删除一个经纪人
	 * @param $uid
	 */
	function deleteBroker($uid) {
		$tempBroker = $this->_brokerDB->get($uid);
		if (!$tempBroker) return false;
		$this->_brokerDB->delete($uid);
		
		$brokerGroupService = $this->_getBrokerGroupService();
		$brokerGroupService->reduceBrokerUsernum($tempBroker['groupid']);
	}
	/**
	 * 批量删除经纪人
	 * @param $uid
	 */
	function deleteBrokers($uids) {
		if (!S::isArray($uids)) return false;
		foreach ($uids as $value) {
			$value = (int) $value;
			if (!$value) continue;
			$this->deleteBroker($value);
		}
	}
	/**
	 * 添加或减少salenum的数量
	 * @param $uid
	 * @param $num
	 */
	function addSaleNum($uid,$num=1) {
		$uid = (int) $uid;
		$num = (int) $num;
		$secondPostService = $this->_getSecondPostService();
		$secondPostService->addSalenum($uid,$num,1);
	}
	/**
	 * 添加或减少hirenum的数量
	 * @param $uid
	 * @param $num
	 */
	function addHireNum($uid,$num=1) {
		$uid = (int) $uid;
		$num = (int) $num;
		$secondPostService = $this->_getSecondPostService();
		$secondPostService->addHirenum($uid,$num,1);
	}
	/**
	 * 将module数据转换成array数据
	 * @param $brokerModule
	 */
	function _cookBrokerModuleToDB($brokerModule) {
		$map = $this->_getBrokerMap();
		$temp = array();
		foreach ($map as $key => $value) {
			$temp[$key] = $brokerModule->{$value};
		}
		return $temp;
	}
	/**
	 * 获取dao和module的映射关系
	 */
	function _getBrokerMap() {
		return array(
			'uid' => 'uid',
			'username' => 'username',
			'groupid' => 'groupid',
			'realname' => 'realname',
			'area' => 'area',
			'ifactive' => 'ifactive',
			'plate' => 'plate',
			'company' => 'company',
			'address' => 'address',
			'email' => 'email',
			'tel' => 'tel',
			'phone' => 'phone',
			'verify' => 'verify',
			'idcard' => 'idcard',
			'idcardpic' => 'idcardpic',
			'alipay' => 'alipay',
			'otherinfo' => 'otherinfo',
			'remark' => 'remark',
			'createtime' => 'createtime',
			'announce' => 'announce',
		    'business' => 'business',
		);
	}
	
	function _checkBrokerModule($brokerModule) {
		return is_object($brokerModule) && get_class($brokerModule) == 'PW_BrokerModule';
	}
	/**
	 * 处理经纪人信息
	 * @param $broker
	 */
	function _cookBroker($broker) {
		if (!S::isArray($broker)) return $broker;
		$broker['verifyinfo'] = $this->_getVerify($broker['verify']);
		return $broker;
	}
	/**
	 * 处理经纪人信息
	 * @param $broker
	 */
	function _cookBrokerModule($brokerModule) {
		if (!$this->_checkBrokerModule($brokerModule)) return $brokerModule;
		
		$brokerModule->verifyInfo = $this->_getVerify($brokerModule->verify);
		return $brokerModule;
	}
	
	function _getVerify($verify) {
		$result = array();
		foreach ($this->getVerifyInfo() as $key => $value) {
			$check = $verify & $key ? 1 : 0;
			$result[$key] = array(
				'name' => $value['name'],
				'icon' =>	$value['icon'],
				'check' => $check
			);
		}
		return $result;
	}
	/**
	 * 各种认证方式,键表示位运算符
	 */
	function getVerifyInfo() {
		return array(
			'1' => array('name'=>'身份证认证','icon'=>'ico_verify.png'),
			'2' => array('name'=>'支付宝认证','icon'=>'ico_alipay.png'),
			'4' => array('name'=>'手机认证','icon'=>'ico_mobile.png'),
			'8' => array('name'=>'其他认证','icon'=>'ico_other.png')
		);
	}
	
	function _getBrokerModule() {
		return House::loadModule('brokermodule');
	}
	/**
	 * 获取经纪人dao
	 * @return PW_House_BrokerDB
	 */
	function _getBrokerDB() {
		return House::loadDB('house_broker');
	}
	/**
	 * 获取经纪人用户组service
	 * @return PW_BrokerGroupService
	 */
	function _getBrokerGroupService() {
		return House::loadService('brokergroupservice');
	}
	/**
	 * 获取二手房发布数量service
	 * @return PW_SecondPostService
	 */
	function _getSecondPostService() {
		return House::loadService('secondpostservice');
	}
}